package com.example.controller;
import com.example.entity.Cinema;
import com.example.entity.Film;
import com.example.service.CinemaService;
import com.example.service.FilmService;
import com.example.vo.CinemaVo;
import com.example.vo.FilmDetailVo;
import com.example.vo.FilmVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class FilmController {

    @Autowired
    private FilmService filmService;

    @Autowired
    private CinemaService cinemaService;


    @RequestMapping("/filmList")
    @ResponseBody
    public List<FilmVo> selectAll() {
        List<FilmVo> filmVos = filmService.selectAll();
        return filmVos;
    }


    @RequestMapping("/")
    public String home(Model model) {
        System.out.println("接收首页请求");
        List<FilmVo> filmVoList = filmService.selectAll();
      //  System.out.println("这里过去了吗？");
        model.addAttribute("filmVoList", filmVoList);
       // System.out.println("到这里了吗？");
        return "home";
    }

    // 通过@RequestParam接收请求参数
    @RequestMapping("/filmInfo")
    public String filmInfo(@RequestParam String filmId, Model model) {
        FilmDetailVo detailVo = filmService.findFilmById(filmId);
        model.addAttribute("detailVo", detailVo);
        return "detail";
    }

    @RequestMapping("/search")
    public String search(Model model, String keyword){
        if(StringUtils.hasText(keyword)){
            model.addAttribute("keyword",keyword);
            List<Film> filmList= filmService.searchFilm(keyword);
            model.addAttribute("filmList",filmList);
        }else {
            List<Film> filmList=filmService.selectAllFilm();
            model.addAttribute("filmList",filmList);
        }

        return "search";
    }


    @RequestMapping("/cinema")
    public String cinemaInfo(Model model){
        List <Cinema> cinemaList=cinemaService.selectAll();
        model.addAttribute("cinemaList",cinemaList);
        return "cinema";
    }

    @RequestMapping("/cinemaShow")
    public String cinemaShow(Model model,String cinemaId){
        /*List<Film> films=filmService.selectFilmByCinemaId(cinemaId);
        model.addAttribute("filmsList",films);
        return "cinema-detail";*/

        List<CinemaVo> films=cinemaService.selectFilmByCinemaId(cinemaId);
        model.addAttribute("filmsList",films);
        return "cinema-detail";
    }
}
